package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.SetmealDao;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 统计报表服务
 */
@Service(interfaceClass = ReportService.class)
@Transactional
public class ReportServiceImpl implements ReportService {

    @Autowired
    private MemberDao memberDao;

    @Autowired
    private OrderDao orderDao;

    @Autowired
    private SetmealDao setmealDao;

    /**
     * 获得运营统计数据
     * Map数据格式：
     * todayNewMember ‐> number
     * totalMember ‐> number
     * thisWeekNewMember ‐> number
     * thisMonthNewMember ‐> number
     * todayOrderNumber ‐> number
     * todayVisitsNumber ‐> number
     * thisWeekOrderNumber ‐> number
     * thisWeekVisitsNumber ‐> number
     * thisMonthOrderNumber ‐> number
     * thisMonthVisitsNumber ‐> number
     * hotSetmeal ‐> List<Setmeal>
     */
    public Map<String, Object> getBusinessReport() throws Exception{

        //获得当前日期
        String today = DateUtils.parseDate2String(DateUtils.getToday());
        //获得本周一的日期
        String thisWeekMonday = DateUtils.parseDate2String(DateUtils.getThisWeekMonday());
        //获得本月第一天的日期
        String firstDay4ThisMonth = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());

        //今日新增会员数
        Integer todayNewMember = memberDao.findMemberCountByDate(today);
        //总会员数
        Integer totalMember = memberDao.findMemberTotalCount();
        //本周新增会员数
        Integer thisWeekNewMember = memberDao.findMemberCountAfterDate(thisWeekMonday);
        //本月新增会员数
        Integer thisMonthNewMember = memberDao.findMemberCountAfterDate(firstDay4ThisMonth);

        //今日预约数
        Integer todayOrderNumber = orderDao.findOrderCountByDate(today);
        //本周预约数
        Integer thisWeekOrderNumber = orderDao.findOrderCountAfterDate(thisWeekMonday);
        //本月预约数
        Integer thisMonthOrderNumber = orderDao.findOrderCountAfterDate(firstDay4ThisMonth);
        //今日到诊数
        Integer todayVisitsNumber = orderDao.findVisitsCountByDate(today);
        //本周到诊数
        Integer thisWeekVisitsNumber =orderDao.findVisitsCountAfterDate(thisWeekMonday);
        //本月到诊数
        Integer thisMonthVisitsNumber = orderDao.findVisitsCountAfterDate(firstDay4ThisMonth);

        //热门套餐（取前4）
        List<Map> hotSetmeal = orderDao.findHotSetmeal();

        Map<String,Object> result = new HashMap<>();
        result.put("reportDate",today);
        result.put("todayNewMember",todayNewMember);
        result.put("totalMember",totalMember);
        result.put("thisWeekNewMember",thisWeekNewMember);
        result.put("thisMonthNewMember",thisMonthNewMember);
        result.put("todayOrderNumber",todayOrderNumber);
        result.put("thisWeekOrderNumber",thisWeekOrderNumber);
        result.put("thisMonthOrderNumber",thisMonthOrderNumber);
        result.put("todayVisitsNumber",todayVisitsNumber);
        result.put("thisWeekVisitsNumber",thisWeekVisitsNumber);
        result.put("thisMonthVisitsNumber",thisMonthVisitsNumber);
        result.put("hotSetmeal",hotSetmeal);
        return result;
    }
    @Override
    //获取后台数据统计
    public Map<String, Object> getOrderReport(String[] value1) {
        if(value1.length==0){
            Map<String,String> map = new HashMap<>();
            map.put("resultsTime",currentTime());
            map.put("startTime",weekAgoTime());
            //查询当天时间内有预约的时间
            List<String> days =orderDao.ordertDateIime(map);
            Map<String, Object> orderReports = getOrderReports(days);
            return orderReports;
        }

        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("resultsTime",value1[1]);
        hashMap.put("startTime",value1[0]);
        List<String> day = orderDao.ordertDateIime(hashMap);
        Map<String, Object> map = getOrderReports(day);
        return map;
    }
    //查询套餐与收入详情
    @Override
    public Map<String, Object> selectSetmealIncome(String[] value1) {
        if (value1.length==0){
            Map<String,String> map = new HashMap<>();
            map.put("resultsTime",currentTime());
            map.put("startTime",weekAgoTime());

            List<String> setmealNames = setmealDao.selectSetmealname(map);
            setmealNames.add("总收入");
            List<Map<String, Object>> setmealCount = setmealDao.selectBySetmealIncome(map);
            HashMap<String, Object> sum = new HashMap<>();
            sum.put("name","总收入");
            sum.put("value",this.sum(setmealCount));
            setmealCount.add(sum);
            HashMap hashMap = new HashMap();
            hashMap.put("setmealNames",setmealNames);
            hashMap.put("setmealCount",setmealCount);
            return hashMap;
        }
        HashMap<String, String> hashMap1 = new HashMap<>();
        hashMap1.put("resultsTime",value1[1]);
        hashMap1.put("startTime",value1[0]);
        HashMap<String, Object> hashMap2 = new HashMap<>();
        List<String> setmealNames = setmealDao.selectSetmealname(hashMap1);
        setmealNames.add("总收入");
        List<Map<String, Object>> setmealCount = setmealDao.selectBySetmealIncome(hashMap1);
        HashMap<String, Object> sum = new HashMap<>();
        sum.put("name","总收入");
        sum.put("value",this.sum(setmealCount));
        setmealCount.add(sum);
        hashMap2.put("setmealNames",setmealNames);
        hashMap2.put("setmealCount",setmealCount);
        return hashMap2;
    }

    //获取总收入
    public double sum(List<Map<String,Object>> list){
        double a = 0;
        for (Map<String, Object> stringObjectMap : list) {
            Set<String> strings = stringObjectMap.keySet();
            for (String string : strings) {
                if("value".equals(string)){
                    Object o = stringObjectMap.get(string);
                    a = a+(double)o;
                }
            }
        }
        return a ;
    }


    //获取后台数据方法
    public Map<String,Object> getOrderReports(List<String> list){
        Map<String, Object> retuenMap = new HashMap<>();
        ArrayList<Integer> orderCount = new ArrayList<>();
        ArrayList<Integer> statusCount = new ArrayList<>();
        for (String day : list) {
            Integer integer = orderDao.selectOrderNumber(day);
            orderCount.add(integer);
            Integer integer1 = orderDao.selectOrderStatusNumber(day);
            statusCount.add(integer1);
        }
        retuenMap.put("days",list);
        retuenMap.put("orderCount",orderCount);//orderCount
        retuenMap.put("statusCount",statusCount);
        return retuenMap;
    }

    //获取七天前时间，格式为“yyyy-MM-dd”
    public String weekAgoTime(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime( new Date());
        calendar.add(Calendar.DATE,-7);
        Date time = calendar.getTime();
        return sdf.format(time);
    }
    //获取当前时间
    public String currentTime(){
        return new SimpleDateFormat("yyyy-MM-dd").format(new Date());
    }
}